import 'package:flutter/material.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';

class ItemEntity {
  late String select;
  late List valueArr;
  late Function onTap;

  ItemEntity(this.select, this.valueArr, this.onTap);
}

class ChooseBox extends StatefulWidget {
  final ItemEntity entity;
  const ChooseBox(this.entity, {Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => _ChooseBoxState();
}

class _ChooseBoxState extends State<ChooseBox> {
  List choose = [];
  @override
  void initState() {
    choose = widget.entity.select.split(' ');
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints(
        maxHeight: 200,
      ),
      child: ListView(
        children: widget.entity.valueArr
            .map(
              (e) => GestureDetector(
                onTap: ClickUtils.debounce(() {
                  if (choose.indexOf(e) > 0) {
                    choose.remove(e);
                  } else {
                    print(choose);
                    print(e);
                    choose.add(e);
                  }
                  print(choose);
                  setState(() {
                    choose = choose;
                  });
                  widget.entity.onTap(choose);
                }),
                child: Container(
                  margin: EdgeInsets.only(top: 10),
                  padding: EdgeInsets.only(left: 16, right: 16),
                  alignment: Alignment.centerLeft,
                  height: 42,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(8),
                    border: Border.all(
                      width: 0.5,
                      color: choose.indexOf(e) > 0
                          ? ThemeColor.primaryColor
                          : ThemeColor.colorBlack20,
                    ),
                    color: choose.indexOf(e) > 0
                        ? ThemeColor.primaryColor
                        : ThemeColor.bkColorFFFFFF,
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('$e'),
                      choose.indexOf(e) > 0 ? Icon(Icons.done) : Text(''),
                    ],
                  ),
                ),
              ),
            )
            .toList(),
      ),
    );
  }
}
